package sioserver;

/**
 * RequestHandler才是真正的任务执行者。执行完后，需要归还资源，以便其他的任务执行者能请求道资源。
 * 
 * @author Xinz
 *
 */
public class SIORequestHandler extends Thread {

	private Integer tid;
	private Integer rid;
	private SIODispatcher dispatcher;
	
	public SIORequestHandler(SIODispatcher dispatcher, Integer tid, Integer rid) {
		this.tid = tid;
		this.rid = rid;
		this.dispatcher = dispatcher;
	}
	
	@Override
	public void run() {
		System.out.println("Request No.[" + tid + "] is handling with resource [" + rid + "]"); 
		try {
			sleep(1000);
		}
		catch(InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Request No.[" + tid + "] is processed with resource [" + rid + "]"); 
		
		// 处理完成后释放资源
		dispatcher.freeResource(rid);
		
		System.out.println("Return resource:" + rid);
	}
}
